import json
from websocket import create_connection
from time import sleep
from threading import Thread

a=create_connection('ws://ubuntu@52.68.85.88:8082/?params=MTA4ODcwMjY2OTgxNzY0NzEwNCxYQlRVU0QsZXlKaGJHY2lPaUpJVXpJMU5pSXNJblI1Y0NJNklrcFhWQ0o5LmV5SjFhV1FpT2lJeE1EZzROekF5TmpZNU9ERTNOalEzTVRBMElpd2laWGh3SWpveE5UUTROVFF5TURBNExDSnliMnhsSWpveGZRLjZJMlVDQy1iclBrMEdKZnlUbXRyZDJOWF9CaDBpVVNSZTZZNlltZ09TVjg=')

i=[0]   # 想要共享数据，yi=0是不行的，列表可以使两个函数通用一个计数器
def recv(i):
        while 1:
                data=a.recv()
                i[0]+=1
                print('这是i',i[0])
                if i[0]==5:
                        print(data)

def send(i):
        while 1:
                if i[0]==100:
                        a.send('ping')
                        i[0]=0
                        print('发送了ping')
'''
以前的单线程测试方法
while 1:
        data_before=a.recv()
        i+=1
        print('这是i',i)
        if i==1200:
                a.send('ping')
                i=0
                print('已发送ping')
        elif i==5:
                print(data_before)
'''
if __name__=='__main__':
        work=Thread(target=recv,args=(i,))
        ping=Thread(target=send,args=(i,))
        work.start()
        ping.start()